<?php

declare(strict_types=1);

namespace app\controller\system\permission;

use app\BaseController;
use app\service\system\UserGroupService;

/**
 * 管理员分组
 */
class Group extends BaseController
{
    protected $groupService;

    public function initialize()
    {
        $this->groupService = make(UserGroupService::class);
    }
    /**
     * 列表
     */
    public function list()
    {
        return $this->success($this->groupService->list($this->paging()));
    }
    /**
     * 编辑
     */
    public function edit()
    {
        $data = $this->request->post();
        $this->validate($data, [
            'name' => 'require|length:2,20',
            'status' => 'integer|in:0,1',
            'sort' => 'integer|egt:1|elt:255',
        ]);
        $res = $this->groupService->edit($data, (int) $data['id']);
        return $res ? $this->success() : $this->error('保存失败');
    }
    /**
     * 删除
     */
    public function delete(int $group_id)
    {
        $del = $this->groupService->delete($group_id);
        return $del ? $this->success() : $this->error('删除失败');
    }
}
